package com.test21_常用类;

import org.junit.Test;

/**
 * Runtime类
 */
public class Demo03_Runtime {

    @Test
    public void test1() throws Exception {
        // Runtime类是单例模式，不能被new实例化
        // Runtime runtime = new Runtime();
        Runtime runtime = Runtime.getRuntime();     // 获取Runtime类的对象实例
        Process process = runtime.exec("calc.exe");     // 执行Windows系统中的命令
        Thread.sleep(2000);
        process.destroy();  // 销毁进程
    }

    @Test
    public void test2() {
        Runtime runtime = Runtime.getRuntime();
        System.out.println("【1】垃圾产生前：MaxMemory = " + runtime.maxMemory());        // 整个JVM进程的可用最大内存
        System.out.println("【1】垃圾产生前：TotalMemory = " + runtime.totalMemory());    // 默认的可用内存
        System.out.println("【1】垃圾产生前：FreeMemory = " + runtime.freeMemory());      // 空闲内存

        String message = "www.yootk.com";
        // for (int x = 0; x < Integer.MAX_VALUE; x++) {
        for (int x = 0; x < 20; x++) {
            message += "\n" + message + x;
        }
        System.out.println("【2】垃圾产生后：MaxMemory = " + runtime.maxMemory());
        System.out.println("【2】垃圾产生后：TotalMemory = " + runtime.totalMemory());
        System.out.println("【2】垃圾产生后：FreeMemory = " + runtime.freeMemory());


        runtime.gc();   // 手工进行垃圾清除
        System.out.println("【3】GC调用后：MaxMemory = " + runtime.maxMemory());
        System.out.println("【3】GC调用后：TotalMemory = " + runtime.totalMemory());
        System.out.println("【3】GC调用后：FreeMemory = " + runtime.freeMemory());
    }
}
